//
//  HomePageDetailController.m
//  DianDangDai
//
//  Created by bhf on 2017/12/15.
//  Copyright © 2017年 bhf. All rights reserved.
//

#import "HomePageDetailController.h"
#import "TabBarController.h"
@interface HomePageDetailController ()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, strong) JSContext *jsContext;
@end

@implementation HomePageDetailController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = _titleStr;
    NSLog(@"%@",_titleStr);
    if (@available(iOS 11.0, *)) {
        self.MyWeb.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
        // Fallback on earlier versions
    }
    
    //进度条
    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 64, [[UIScreen mainScreen] bounds].size.width, 2)];
    self.progressView.backgroundColor = [UIColor blueColor];
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    [self.view addSubview:self.progressView];
   
    WKUserContentController *userContentController = [[WKUserContentController alloc] init];
    [userContentController addScriptMessageHandler:self name:@"ipsLoad"];
    [userContentController addScriptMessageHandler:self name:@"ipsRightBtn"];
    [userContentController addScriptMessageHandler:self name:@"ipsLeftBtn"];
    [userContentController addScriptMessageHandler:self name:@"czSuccess"];
    // WKWebView的配置
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    configuration.userContentController = userContentController;
  
    
    self.MyWeb = [[WKWebView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64) configuration:configuration];
    
    self.MyWeb.navigationDelegate = self;
    self.MyWeb.UIDelegate = self;
    
    //self.MyWeb.scrollView.pagingEnabled = YES;
    self.MyWeb.scrollView.scrollsToTop = NO;
    self.MyWeb.scrollView.bounces = NO;

    
    
    NSLog(@"%@",_filepath);
    [self.view addSubview:self.MyWeb];
    if (@available(iOS 9.0, *)) {
        NSURLRequest *request = [NSURLRequest requestWithURL:_filepath];
        [self.MyWeb loadRequest:request];
   
    } else {
        // Fallback on earlier versions
    }
    [self.MyWeb addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.progress = self.MyWeb.estimatedProgress;
        if (self.progressView.progress == 1) {
            /* *添加一个简单的动画，将progressView的Height变为1.4倍，在开始加载网页的代理中会恢复为1.5倍 *动画时长0.25s，延时0.3s后开始动画 *动画结束后将progressView隐藏 */
            __weak typeof (self)weakSelf = self;
            [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{ weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
            } completion:^(BOOL finished) {
                weakSelf.progressView.hidden = YES;
            }];
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    //开始加载网页时展示出progressView
    self.progressView.hidden = NO;
    //开始加载网页的时候将progressView的Height恢复为1.5倍
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    //防止progressView被网页挡住
    [self.view bringSubviewToFront:self.progressView];
    
}


- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    self.progressView.hidden = YES;
    NSLog(@"%@",self.MyWeb.URL);
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{

    ZCAlertViewController *alert = [ZCAlertViewController alertControllerWithTitle:@"提示" message:@"加载失败，是否重新加载"];
    ZCAlertAction *action = [ZCAlertAction actionWithTitle:@"确定" handler:^(ZCAlertAction *action) {
        [self.MyWeb reload];
        self.progressView.hidden = YES;
    }];
    ZCAlertAction *cancel = [ZCAlertAction actionWithTitle:@"取消" handler:^(ZCAlertAction *action) {
        
    }];
    [alert addAction:action];
    [alert addAction:cancel];
    [self presentViewController:alert animated:NO completion:nil];
    
}


- (void)ipsLoad:(int)code {
   
    NSLog(@"1111");
}

- (void)ipsRightBtn:(int)code {


}

- (void)ipsLeftBtn:(int)code {
    
}

- (void)czSuccess:(int)code {
    
}


- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    NSLog(@"方法名:%@", message.name);
    NSLog(@"参数:%@", message.body);
    if ([message.name isEqualToString:@"ipsLoad"]) {
       
    }
    if ([message.name isEqualToString:@"ipsRightBtn"]) {
        for (UIViewController *vcHome in self.navigationController.viewControllers) {
            if ([vcHome isKindOfClass:[TabBarController class]]) {
                [self.navigationController popToViewController:vcHome animated:YES];
            }
        }
    }
    if ([message.name isEqualToString:@"ipsLeftBtn"]) {
       
    }
    if ([message.name isEqualToString:@"czSuccess"]) {
        
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_MyWeb removeObserver:self forKeyPath:@"estimatedProgress" context:nil];

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
